/* Copyright 2013-2015 www.snakerflow.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.model;

import org.snaker.engine.Action;
import org.snaker.engine.core.Execution;
import org.snaker.engine.handlers.impl.CreateTaskHandler;
import org.snaker.engine.handlers.impl.StartSubProcessHandler;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 变迁定义transition元素
 * 
 * @author yuqs
 * @since 1.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class TransitionModel extends BaseModel implements Action {
    private static final long serialVersionUID = 3688123410411321158L;
    private NodeModel source; // 变迁的源节点引用
    private NodeModel target; // 变迁的目标节点引用
    private String to; // 变迁的目标节点name名称
    private String expr; // 变迁的条件表达式，用于decision
    private String g; // 转折点图形数据
    private String offset; // 描述便宜位置
    private boolean enabled = false; // 当前变迁路径是否可用

    public void execute(Execution execution) {
        if (!enabled)
            return;
        if (target instanceof TaskModel) {
            // 如果目标节点模型为TaskModel，则创建task
            fire(new CreateTaskHandler((TaskModel) target), execution);
        } else if (target instanceof SubProcessModel) {
            // 如果目标节点模型为SubProcessModel，则启动子流程
            fire(new StartSubProcessHandler((SubProcessModel) target), execution);
        } else {
            // 如果目标节点模型为其它控制类型，则继续由目标节点执行
            target.execute(execution);
        }
    }
}
